package com.juyu.miliao.api.dao.redis;

import com.juyu.miliao.api.util.RedisKeys;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: BeanWelfareRedisDao
 * @Description: TODO
 * @Author zhangcheng
 * @Date 19-9-18上午9:10
 */
@Repository
public class BeanWelfareRedisDao {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public List<Integer> getAwardStatusList(Long userId) {
        List<Integer> statusList = new ArrayList<>(Arrays.asList(0, 0, 0, 0));
        stringRedisTemplate.boundSetOps(RedisKeys.BEAN_WELFARE.getValue(userId))
                .members()
                .stream()
                .map(Integer::parseInt)
                .forEach(rewardId -> {
                    statusList.set(rewardId, 2);
                });
        return statusList;
    }

    public boolean award(Long userId, Long rewardId) {
        long result = stringRedisTemplate.boundSetOps(RedisKeys.BEAN_WELFARE.getValue(userId))
                .add(String.valueOf(rewardId));
        return result == 1L;
    }

}
